package aop03;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.Method;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/5/18 9:32
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class AroundAdvice implements MethodInterceptor {

    /**
     * 环绕通知
     * @param invocation
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        // 目标类
        Object target = invocation.getThis();
        // 目标方法
        Method method = invocation.getMethod();
        // 方法参数列表
        Object[] args = invocation.getArguments();

        Object result = null;


        try {
            System.out.println("环绕通知之前置通知");
            long begin = System.currentTimeMillis();
            // 执行目标方法
            result = invocation.proceed();
            long end = System.currentTimeMillis();
            // 计算执行目标方法共耗费了多少毫秒
            System.out.println("环绕通知之后置通知,方法返回值为:"+result+",执行方法共耗费了"+(end - begin)+"毫秒");
        } catch (Throwable throwable) {
            System.out.println("环绕通知之异常通知,方法异常为:"+throwable);
        }

        return result;
    }
}
